﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCDanhSachMon : UserControl
    {
        MonHocBUO mhBUO = new MonHocBUO();

        int id;
        String id_Mon, id_Khoi;

        public UCDanhSachMon()
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = mhBUO.HienThiMon();
            
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (rdTimTheoTenMon.Checked == true)
            {
                if (txtTimTheoTenMon.Text == "")
                {
                    MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = mhBUO.TimTheotenMon(txtTimTheoTenMon.Text);
                }
            }
            else
            {
                if (cbbTimTheoKhoi.Text == "")
                {
                    MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    dataGridView1.AutoGenerateColumns = false;
                    dataGridView1.DataSource = mhBUO.TimTheoKhoi(cbbTimTheoKhoi.Text);
                }
            }
        }

        private void rdTimTheoTenMon_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTenMon.Enabled = true;
            cbbTimTheoKhoi.Enabled = false;
            dataGridView1.AutoGenerateColumns = false;
            //dataGridView1.DataSource = mhBUO.HienThiMon();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtTimTheoTenMon.Enabled = false;
            cbbTimTheoKhoi.Enabled = true;
            showKhoi();
            //dataGridView1.DataSource = mhBUO.HienThiMon();
        }

        private void showKhoi()
        {
            tbl_Khoi tbl_Khoi = (tbl_Khoi)cbbTimTheoKhoi.SelectedItem;
            cbbTimTheoKhoi.DataSource = mhBUO.getKhoi();
            cbbTimTheoKhoi.DisplayMember = "TenKhoi";
            cbbTimTheoKhoi.ValueMember = "id_Khoi";
        }

        private void txtTimTheoTenMon_TextChanged(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = mhBUO.TimTheotenMon(txtTimTheoTenMon.Text);
        }

        private void txtTimTheoTenMon_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rdTimTheoTenMon.Checked == true)
                {
                    if (txtTimTheoTenMon.Text.Equals(""))
                    {
                        MessageBox.Show("Chưa nhập tên môn học cần tìm");
                    }
                    else
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = mhBUO.TimTheotenMon(txtTimTheoTenMon.Text);
                        dataGridView1.Visible = true;
                    }
                }
            }
        }

        private void cbbTimTheoKhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = mhBUO.TimTheoKhoi(cbbTimTheoKhoi.Text);
        }

        private void thêmMônToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemMon a = new UCThemMon();
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void sửaMônToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCSuaMon a = new UCSuaMon(id_Khoi, id_Mon);
            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_Mon = dataGridView1.Rows[id].Cells[0].Value.ToString();
                        id_Khoi = dataGridView1.Rows[id].Cells[1].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void xóaMônToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu giáo viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (mhBUO.Delete(id_Mon))
                {
                    MessageBox.Show("Xóa môn thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa môn thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = mhBUO.HienThiMon();
            }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            MonHocReport r = new MonHocReport();
            r.DataSource = dataGridView1.DataSource;
            r.Load();
            r.ShowPreviewDialog();
        }
    }
}
